package com.zyb.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyb.commons.service.impl.CrudServiceImpl;
import com.zyb.product.dao.CategoryDao;
import com.zyb.product.dto.CategoryDTO;
import com.zyb.product.entity.CategoryEntity;
import com.zyb.product.service.CategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 商品三级分类
 *
 * @author 赵毅博 593163583@qq.com
 * @since 1.0.0 2022-07-15
 */
@Service
public class CategoryServiceImpl extends CrudServiceImpl<CategoryDao, CategoryEntity, CategoryDTO> implements CategoryService {

    @Override
    public QueryWrapper<CategoryEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public List<CategoryEntity> listTree() {
//        查出所有分类，组装数据，封装成3级分类
        List<CategoryEntity> list = baseDao.selectList(null);
//        组装父子树形结构
//        思路，先找一级分类(特点parent_id为0)
        List<CategoryEntity> level1Menu = list.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == 0;
        }).map(menu -> {
            menu.setChildren(getChildrens(menu,list));
            return menu;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort() == null ? 0: menu1.getSort()) - (menu2.getSort() ==null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return level1Menu;
    }

    @Override
    public void removeMenuIds(List<Long> asList) {
//        TODO 需要检查是否正在被引用
        baseDao.deleteBatchIds(asList);
    }

    @Override
    public Long[] getFatherIds(Long catId) {
        List<Long> paths = new ArrayList<>();
        List<Long> allPath = this.getParents(catId, paths);
        Collections.reverse(allPath);

        return  allPath.toArray(new Long[allPath.size()]);
    }

    //    递归查找某个菜单的子菜单
    private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == root.getCatId();
        }).map(categoryEntity -> {
            categoryEntity.setChildren(getChildrens(categoryEntity,all));
            return categoryEntity;
        }).sorted((menu1,menu2) -> {
//            在此处会有异常，因为sort的默认值可能是空，需要添加默认值，防止报空，通过三目
            return (menu1.getSort() == null ? 0: menu1.getSort()) - (menu2.getSort() ==null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

    //    递归查找某个菜单的父菜单
    private List<Long> getParents(Long catId,List<Long> paths){
        CategoryDTO category = this.get(catId);
        paths.add(catId);
        if (category.getParentCid() != 0 ){
            getParents(category.getParentCid(),paths);
        }
        return paths;
    }

}